package com.ews.onlineexchange.controller;

import java.util.ArrayList;
import java.util.List;

import javax.enterprise.inject.Model;
import javax.persistence.EntityGraph;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;

import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.DataEntity;
import com.ews.onlineexchange.model.TradeStatus;
import com.ews.onlineexchange.service.EntityQuery;

@Model
public class BulletinArticleList extends EntityQuery<Bulletin> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public BulletinArticleList(){
		
		setMaxResults(MAX_RESULTS_NORMAL);
		setOrder("createDate DESC");
		
	}
	private String searchString;
	@Override
	protected List<Predicate> getRestrictions(CriteriaBuilder cb, Root<Bulletin> entity) {
		List<Predicate> restrictions = new ArrayList<Predicate>();
		restrictions.add(cb.equal(entity.get("delFlag"), DataEntity.DEL_FLAG_NORMAL));
		restrictions.add(cb.isTrue(entity.<Boolean>get("auditResult")));
//		restrictions.add(cb.isNotTrue(entity.get("auditResult")));
		if(StringUtils.isNotEmpty(searchString)) restrictions.add(cb.like(entity.get("projectName"),"%"+searchString+"%"));
		return restrictions;
	}
	public String getSearchString() {
		return searchString;
	}
	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}
	
	@Override
	protected void setHint( TypedQuery<Bulletin> query ) {
		EntityGraph<?> entityGraph = entityManager.getEntityGraph( "Bulletin.QualificationGroup" );
		query.setHint( "javax.persistence.loadgraph", entityGraph );		
	}

}
